/**
 * Aistarfish.com Inc.
 * Copyright (c) 2017-2019 All Rights Reserved.
 */
package com.xf.service;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.qingstor.sdk.exception.QSException;
import com.xf.dao.AppDao;
import com.xf.model.App;
import com.xf.model.PageParam;

/**
 *
 * @author xiangfeng
 * Created by on 2019-12-17 17:39
 */
public class AppService {

    @Inject
    private AppDao appDao;

    @Inject
    private FileService fileService;

    public List<App> getAllPus() {

        List<App> allPus = appDao.getAllPus();
        urlTransfer(allPus);

        return allPus;
    }

    public void addApp(UploadFile logo, String logoUrl, UploadFile ico, String icoUrl, String name,
                       String redirectUrl, Boolean open, Integer type)
            throws IOException, QSException {
        App app = new App();
        appFileSwapper(app, logo,logoUrl, ico,icoUrl);

        app.setName(name);
        app.setRedirectUrl(redirectUrl);
        app.setOpen(open);
        app.setType(type);
        appDao.addApp(app);
    }

    private String getFileSubfix(String fileName){
        if(fileName.contains(".")){
            return fileName.substring(fileName.indexOf("."));
        }
        return "";
    }

    public List<App> getAll() {
        List<App> allPus = appDao.getAll();
        urlTransfer(allPus);

        return allPus;
    }

    private void urlTransfer(List<App> allPus) {
        allPus.forEach(item -> {
            if (StringUtils.isNotBlank(item.getIco()) && !item.getIco().startsWith("http")) {
                item.setIco(fileService.getAccessUrl(item.getIco(), null));
            }
            if (StringUtils.isNotBlank(item.getLogo()) && !item.getLogo().startsWith("http")) {
                item.setLogo(fileService.getAccessUrl(item.getLogo(), null));
            }
        });
    }

    public void update(App app, UploadFile logo, String logoUrl, UploadFile ico, String icoUrl) throws IOException, QSException {
        appFileSwapper(app, logo, logoUrl, ico, icoUrl);
        appDao.update(app);
    }

    private void appFileSwapper(App app, UploadFile logo, String logoUrl, UploadFile ico,
                                String icoUrl)
            throws QSException, IOException {

        if (logo != null && StringUtils.isBlank(logoUrl)) {
            logoUrl = fileService.putObject("image/jpeg", logo.getFile(),
                    "mr/app-store/" + DigestUtils.md5Hex(new FileInputStream(logo.getFile()))
                    + getFileSubfix(logo.getOriginalFileName()), logo.getFile().length());
        }
        app.setLogo(logoUrl);
        if (ico != null && StringUtils.isBlank(icoUrl)) {
            icoUrl = fileService.putObject("image/jpeg", ico.getFile(),
                    "mr/app-store/" + DigestUtils.md5Hex(new FileInputStream(ico.getFile()))
                    + getFileSubfix(ico.getOriginalFileName()), ico.getFile().length());
        }
        app.setIco(icoUrl);
    }

    public void delApp(Integer id) {
        appDao.del(id);
    }

    public Page<App> getAppByPage(PageParam pageParam) {
        return appDao.getAppByPage(pageParam);
    }
}
